<?php
namespace app\index\model;

use app\common\model\SuperModel;
use think\Model;
use think\Request;

class White extends SuperModel {



	//关闭update_time自动写入字段
	protected $updateTime = false;

	//重写父类初始化方法
	protected function initialize() {

		parent::initialize();
		$this->table = 'white';
	}

	public function doAdd(Request $request) {

		try {

			//中国时区
			date_default_timezone_set('PRC');

			if ('account' == input('type')) {
				$servers = explode(",", input('server'));
				$list = [];
				foreach ($servers as $server) {

					$accounts = explode(',', input('account'));

					foreach ($accounts as $account) {
						$rec = [];
						$rec['type'] = input('type');
						$rec['server'] = $server;
						$rec['account'] = $account;
						$rec['create_time'] = date("Y-m-d H:i:s");

						array_push($list, $rec);
					}
				}
				$res = $this->allowField(true)
					->saveAll($list);
					
			} else if ('ip' == input('type')) {
				$servers = explode(",", input('server'));
				$list = [];
				foreach ($servers as $server) {

					$rec = [];
					$rec['type'] = input('type');
					$rec['server'] = $server;
					$rec['ip_from'] = input('ip_from');
					$rec['ip_end'] = input('ip_end');
					$rec['create_time'] = date("Y-m-d H:i:s");

					array_push($list, $rec);
				}
				$res = $this->allowField(true)
					->saveAll($list);
			}
			//数据插入
			$output = array('success' => $res ? true : false, 'msg' => $res ? "新增成功！" : $this->getError());

			$this->doLog($output);

			_callback($output);

			exit;

		} catch (\Exception $e) {

			$this->doLog($e->getMessage());
			throw new \think\Exception('异常消息:' . $e->getMessage());
		}
	}
}
